//
//  CXHomeViewController.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/26.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXHomeViewController.h"
#import "CXHotViewController.h"
//#import "CXClassifyViewController.h"
//#import "CXBoutiqueViewController.h"
//#import "CXLiveViewController.h"
#import "ZXClassifyViewController.h"
#import "ZXBoutiqueViewController.h"
#import "ZxFmHomeLiveViewController.h"


#import "CXBroadcastViewController.h"

#import "CXTitleButton.h"
#import "CXConst.h"
#import "UIBarButtonItem+CXItem.h"

#import "UIView+CXFrame.h"

@interface CXHomeViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIView *titleView;

@property (nonatomic, weak) UIScrollView * scrollView;

@property(nonatomic ,weak)  CXTitleButton* selectionTitleButton;

@property (nonatomic, weak) UIView *indicatorView;

@end

@implementation CXHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBar];
    
//    self.automaticallyAdjustsScrollViewInsets = NO;

    //添加子控制器
    [self cx_setupChildViewControllers];
    //添加ScrollView
    [self cx_setupScrollView];
    //添加标题栏文字
    [self cx_setupTitleView];
    
    [self addChildViewController];
    
}

/**
 添加子控制器
 */
-(void)cx_setupChildViewControllers{
    
    //热门
    CXHotViewController * hotVC = [[CXHotViewController alloc] init];
    [self addChildViewController:hotVC];
    
    //分类
    ZXClassifyViewController * classifyVC = [[ZXClassifyViewController alloc] init];
    [self addChildViewController:classifyVC];
    
    //精品
    ZXBoutiqueViewController * boutiqueVC = [[ZXBoutiqueViewController alloc] init];
    [self addChildViewController:boutiqueVC];
    
    //直播
    ZxFmHomeLiveViewController * liveVC = [[ZxFmHomeLiveViewController alloc] init];
    [self addChildViewController:liveVC];
    
    //广播
    CXBroadcastViewController * broadcastVC = [[CXBroadcastViewController alloc] init];
    [self addChildViewController:broadcastVC];
}

-(void)cx_setupScrollView{
    
    self.automaticallyAdjustsScrollViewInsets = YES;

    UIScrollView * scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView = scrollView;
    
    scrollView.delegate = self;
    scrollView.frame = self.view.bounds;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    
    scrollView.bounces = NO;
    
    [self.view addSubview:scrollView];
    
    scrollView.contentSize = CGSizeMake(self.view.width * self.childViewControllers.count, 0);
    
}


/**
 设置标题栏文字
 */
-(void)cx_setupTitleView{
    UIView * titleView = [[UIView alloc] init];
    self.titleView = titleView;
    titleView.backgroundColor = [UIColor whiteColor];
    titleView.frame = CGRectMake(0, 0, self.view.width, 35);
    [self.view addSubview:titleView];
    
    NSArray * titleContens = @[@"热门",@"分类",@"精品",@"直播",@"广播"];
    NSInteger count = titleContens.count;
    
    CGFloat titleButtonW = titleView.width / count;
    CGFloat titleButtonH = titleView.height;
    
    for (NSInteger i = 0; i < count; i++) {
        CXTitleButton * titleButton = [CXTitleButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        
        [titleButton addTarget:self action:@selector(cx_titleClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [titleButton setTitle:titleContens[i] forState:UIControlStateNormal];
        
        CGFloat titleX = i * titleButtonW;
        
        titleButton.frame = CGRectMake(titleX, 0, titleButtonW, titleButtonH);
        
        [titleView addSubview:titleButton];
    }
    //设置选中按钮颜色
    CXTitleButton * firstTitleButton = titleView.subviews.firstObject;
    
    UIView * indicatorView = [[UIView alloc] init];
    self.indicatorView = indicatorView;
    indicatorView.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    
    indicatorView.height = 2;
    indicatorView.y = titleView.height - indicatorView.height;
    
    [titleView addSubview:indicatorView];
    
    //默认选择第一个titleButton
    [firstTitleButton.titleLabel sizeToFit];
    indicatorView.width = firstTitleButton.titleLabel.width;
    indicatorView.centerX = firstTitleButton.centerX;
    
    [self cx_titleClick:firstTitleButton];
    
}

-(void)cx_titleClick:(CXTitleButton *)titleButton{
    
    if (self.selectionTitleButton == titleButton) {
        [[NSNotificationCenter defaultCenter] postNotificationName:CXTitleButtonDidRepeatShowClickNotificationCenter object:nil];
        
    }
    //控制状态
    self.selectionTitleButton.selected = NO;
    titleButton.selected = YES;
    self.selectionTitleButton = titleButton;
    
    //指示器
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = titleButton.titleLabel.width;
        self.indicatorView.centerX = titleButton.centerX;
        
    }];
    
    //让ScrollView移动
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = self.scrollView.width * titleButton.tag;
    [self.scrollView setContentOffset:offset animated:YES];
}


-(void)setupNavBar{
    
    UIBarButtonItem * leftItem = [UIBarButtonItem itemWithImage:kGetImage(@"top_message_n") highlightedImage:kGetImage(@"top_message_n") target:self action:@selector(right)];
    
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //历史
    UIBarButtonItem * settingItem = [UIBarButtonItem itemWithImage:kGetImage(@"top_history_n") highlightedImage:kGetImage(@"top_history_n") target:self action:@selector(setting)];
    //下载
    UIBarButtonItem * nightItem = [UIBarButtonItem itemWithImage:kGetImage(@"top_download_n") selectedImage:kGetImage(@"top_download_n") target:self action:@selector(night:)];
    
    self.navigationItem.rightBarButtonItems = @[nightItem,settingItem];
    
    self.navigationItem.title = @"首页";
}

-(void)right{
    
}

-(void)setting{
    
}


-(void)night:(UIBarButtonItem *)btn{
    
    
}

#pragma mark - 添加自控制的View
-(void)addChildViewController{
    NSInteger index = self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;
    
    //取出自身控制器
    UIViewController * childVc = self.childViewControllers[index];
    //判断添加后就不用添加
    if (childVc.view.superview) {
        return;
    }
    childVc.view.frame = CGRectMake(index * self.scrollView.width, 35, self.scrollView.width, self.scrollView.height);
    [self.scrollView addSubview:childVc.view];
    
}



#pragma mark - UIScrollViewDelegate

/**
 停止移动后调用
 */

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self addChildViewController];
}

/**
 滚动结束时调用
 */
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //点击对应的按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    CXTitleButton * titleButton = self.titleView.subviews[index];
    
    [self cx_titleClick:titleButton];
    
    [self addChildViewController];
}


@end
